﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.ServiceProcess;
using System.Text;

namespace Wolfut.MirageEE.ServiceHost
{
    public partial class WolfutMirageEEService : ServiceBase
    {
        private ServerUtility service = new ServerUtility();
        public WolfutMirageEEService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }
    public class ServerUtility : Component
    {
        private TcpChannel chan = null;
        private int _Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
        private string _RegisterMethod = System.Configuration.ConfigurationManager.AppSettings["Method"];
        //private int _Port = 10024;
        //private string _RegisterMethod = "CacheManage";
        private bool _Active = false;

        /// <summary>
        /// 重载
        /// </summary>
        public ServerUtility()
            : base()
        {

        }

        /// <summary>
        /// 绑定端口
        /// </summary>
        public int Port
        {
            get
            {
                return this._Port;
            }
            set
            {
                this._Port = value;
            }
        }

        /// <summary>
        /// 绑定注册方法名
        /// </summary>
        public string RegisterMethod
        {
            get
            {
                return this._RegisterMethod;
            }
            set
            {
                this._RegisterMethod = value;
            }
        }

        /// <summary>
        /// 获取激活状态
        /// </summary>
        public bool Active
        {
            get
            {
                return this._Active;
            }
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        public void Start()
        {
            try
            {
                chan = new TcpChannel(this._Port);
                ChannelServices.RegisterChannel(chan);
               // RemotingConfiguration.RegisterWellKnownServiceType(typeof(Service.RemotingService), this._RegisterMethod, WellKnownObjectMode.SingleCall);
                this._Active = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                this._Active = false;
            }
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        public void Stop()
        {
            this._Active = false;
            if (chan != null)
            {
                ChannelServices.UnregisterChannel(chan);
            }
        }

    }
}
